package cn.bluemobi.platform.controller.category;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.controller.PlatformBaseController;
import cn.bluemobi.platform.core.dto.DatatablePage;
import cn.bluemobi.platform.entity.category.CommodityCategory;
import cn.bluemobi.platform.service.CatagoryService;

/**
 * 
 * @author qifangke
 * 
 */
@Controller
@RequestMapping("/admin/catagory")
public class CatagoryController extends PlatformBaseController {

	@Autowired
	private CatagoryService catagoryService;

	@RequestMapping("/toFirstCatagoryPage")
	public String toFirstCatagoryPage(ModelMap model) {
		return "catagory/catagoryList";
	}

	@RequestMapping("/findCatagorys")
	@ResponseBody
	public DatatablePage findCatagorys(PageCondition cond) {
		return new DatatablePage(cond, catagoryService.findCatagoryList(cond));
	}

	@RequestMapping("/toCatagoryDetailPage")
	public String toCatagoryDetailPage(
			@RequestParam(value = "catagoryId", required = false) String catagoryId,
			ModelMap model) {
		if (!StringUtils.isEmpty(catagoryId)) {
			CommodityCategory catagory = catagoryService
					.findCategoryDetail(catagoryId);
			model.put("catagory", catagory);
		}
		return "catagory/catagoryDetail";
	}

	@RequestMapping("/saveCatagory")
	@ResponseBody
	public MapDto saveCatagory(CommodityCategory catagory) {
		return catagoryService.saveOrUpdate(catagory, getUser());
	}

	@RequestMapping("/deleteCatagory")
	@ResponseBody
	public MapDto deleteCatagory(@RequestParam("catagoryId") Long catagoryId) {
		return catagoryService.deleteCatagory(catagoryId, getUser());
	}

	@RequestMapping("/toSecondCatagoryPage")
	public String toSecondCatagoryPage(ModelMap model) {
		return "catagory/catagorySecondList";
	}

	@RequestMapping("/findSecondCatagorys")
	@ResponseBody
	public DatatablePage findSecondCatagorys(PageCondition cond) {
		return new DatatablePage(cond,
				catagoryService.findSecondCatagoryList(cond));
	}

	@RequestMapping("/toSecondCatagoryDetailPage")
	public String toSecondCatagoryDetailPage(
			@RequestParam(value = "catagoryId", required = false) String catagoryId,
			ModelMap model) {
		if (!StringUtils.isEmpty(catagoryId)) {
			CommodityCategory catagory = catagoryService
					.findCategoryDetail(catagoryId);
			model.put("catagory", catagory);
		}
		model.addAttribute("catagorys", catagoryService.findAllFirstCategory());
		return "catagory/catagorySecondDetail";
	}

	@RequestMapping("/querySecondCatagorysForAjax")
	@ResponseBody
	public MapDto querySecondCatagorysForAjax(ModelMap model, PageCondition cond) {
		MapDto dto = new MapDto();
		List<CommodityCategory> ccLsit = catagoryService
				.querySecondCatagoryForAjax();
		dto.putInData("catagorys", ccLsit);
		return dto;
	}

}
